package com.heifeng.cloudshelf.mvp.programme.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.event.DialogDismissEvent;
import com.heifeng.cloudshelf.event.MoreClickEvent;
import com.heifeng.cloudshelf.mvp.model.ProgrammeSelectInfo;
import com.heifeng.cloudshelf.widget.BottomDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by Administrator on 2019/9/14.
 */

public class ProgrammeBottomStringRvAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private BottomDialog bottomSheet;

    private int selectPosition = 0;

    public int getSelectPosition() {
        return selectPosition;
    }

    private List<ProgrammeSelectInfo> allData;

    private int Postion;

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    public ProgrammeBottomStringRvAdapter(Context context, int layoutResId, @Nullable List<String> data, List<ProgrammeSelectInfo> allData, int Postion) {
        super(layoutResId, data);
        mContext = context;
        this.allData = allData;
        this.Postion = Postion;

        initBottomDialog();
    }

    @Override
    protected void convert(@NonNull final BaseViewHolder helper, final String item) {
        if (helper.getPosition() == selectPosition) {
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_chosen_sel));
        } else {
            helper.getView(R.id.l_string).setBackground(mContext.getResources().getDrawable(R.mipmap.ic_unchosen_sel));
        }

        if (item.contains("更多")) {
            helper.getView(R.id.iv_more).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_text, "更多");
        } else {
            helper.getView(R.id.iv_more).setVisibility(View.GONE);
//            helper.setText(R.id.tv_text, item);
            if (allData.get(Postion).getOptions().size() > helper.getPosition()) {
                helper.setText(R.id.tv_text, allData.get(Postion).getOptions().get(helper.getPosition()).getSpec_name());
            }
//            helper.setText(R.id.tv_text, allData.get(Postion).getOptions().get(helper.getPosition()).getOptionName());
        }

        helper.getView(R.id.l_string).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.contains("更多")) {
                    EventBus.getDefault().post(new MoreClickEvent());

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            bottomSheet.show();
                        }
                    }, 500);    //延时
                    return;
                }

                setSelectPosition(helper.getPosition());
                notifyDataSetChanged();
            }
        });
    }

    private LinearLayoutManager layoutManager;

    private DialogProgrammeBottomRvAdapter adapter;

    //初始化底部弹窗
    private void initBottomDialog() {
        bottomSheet = new BottomDialog(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_bottom_dialog_rv, null, false);
        bottomSheet.setCancelable(true);
        bottomSheet.setContentView(view);
        bottomSheet.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(R.color.bg_80000000);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvFinish = (TextView) view.findViewById(R.id.tv_finish);

        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new DialogProgrammeBottomRvAdapter(mContext, R.layout.layout_programme_bottom_rv_dialog, allData);
        recyclerView.setAdapter(adapter);

        bottomSheet.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                EventBus.getDefault().post(new DialogDismissEvent());
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheet.dismiss();
            }
        });

        tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheet.dismiss();
            }
        });
    }
}
